package com.jwdf.uda.ui.fragment.user.emoji;

import android.text.InputFilter;
import android.text.Spanned;

import com.jwdf.uda.utils.UdaUtils;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by Administrator on 2016/7/6.
 */
public class EmojiInputFilter implements InputFilter {

    private static Set<String> mFilterSet = null;

    private static void addUnicodeRangeToSet(Set<String> set, int start, int end) {
        if (set == null) {
            return;
        }
        if (start > end) {
            return;
        }

        for (int i = start; i <= end; i++) {
            mFilterSet.add(new String(new int[]{
                    i
            }, 0, 1));
        }
    }

    static {
        mFilterSet = new HashSet<>();

        addUnicodeRangeToSet(mFilterSet, 0x1F601, 0X1F64F);
        addUnicodeRangeToSet(mFilterSet, 0x2702, 0X27B0);
        addUnicodeRangeToSet(mFilterSet, 0X1F680, 0X1F6C0);
        addUnicodeRangeToSet(mFilterSet, 0X24C2, 0X1F251);
        addUnicodeRangeToSet(mFilterSet, 0X1F600, 0X1F636);
        addUnicodeRangeToSet(mFilterSet, 0X1F681, 0X1F6C5);
        addUnicodeRangeToSet(mFilterSet, 0X1F30D, 0X1F567);
    }

    public EmojiInputFilter() {
        super();
    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart,
                               int dend) {
        if (mFilterSet.contains(source.toString())) {
            return "";
        }
        return UdaUtils.filterEmoji(source.toString());
    }

}
